package com.sunfeng.chap2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *   int write(ByteBuffer src)方法 将ByteBuffer的remaining写入通道
 */
public class Test1_2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream(new File("d:\\abc.txt"));
        FileChannel fileChannel = fos.getChannel();


        try {
            ByteBuffer byteBuffer = ByteBuffer.wrap("abcde".getBytes());
            ByteBuffer byteBuffer2 = ByteBuffer.wrap("123456".getBytes());

            fileChannel.write(byteBuffer);

            byteBuffer2.position(2);
            byteBuffer2.limit(5);
           fileChannel.position(2);
            fileChannel.write(byteBuffer2);


            System.out.println(fileChannel.write(byteBuffer));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileChannel.close();
            fos.close();
        }
    }
}
